package com.ytjj.qmyx.supplychain.service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ytjj.common.dto.mqdto.SmsDto;
import com.ytjj.qmyx.supplychain.common.constants.QueueConstants;
import com.ytjj.qmyx.supplychain.common.constants.RedisConstants;
import com.ytjj.qmyx.supplychain.common.model.request.MengWangRequestVo;
import com.ytjj.qmyx.supplychain.common.model.request.ReturnOrderStatisticsRequest;
import com.ytjj.qmyx.supplychain.common.model.request.SanTongRequestVo;
import com.ytjj.qmyx.supplychain.common.model.request.XinycSmsRequest;
import com.ytjj.qmyx.supplychain.common.utils.RedisService;
import com.ytjj.qmyx.supplychain.mapper.ReturnGoodsStaticsMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.ReturnGoodStatisticsSearch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author jiawei
 * @date 2023年03月14日 14:08
 * @describe 消息队列发送业务，消息接收逻辑在销售端的data服务
 */
@Service
@Slf4j
public class MqProducerService {

    @Resource(name="guchuangRabbitTemplate")
    private RabbitTemplate guchuangRabbitTemplate;

//    @Resource(name = "jumaiduoRabbitTemplate")
//    private RabbitTemplate jumaiduoRabbitTemplate;
//
//    @Resource(name = "yiquanRabbitTemplate")
//    private RabbitTemplate yiquanRabbitTemplate;
//
//    @Resource(name = "zxlpRabbitTemplate")
//    private RabbitTemplate zxlpRabbitTemplate;
//
//    @Resource(name = "ygyxRabbitTemplate")
//    private RabbitTemplate ygyxRabbitTemplate;
//
//    @Resource(name = "yxhdRabbitTemplate")
//    private RabbitTemplate yxhdRabbitTemplate;
//
//    @Resource(name = "jckjRabbitTemplate")
//    private RabbitTemplate jckjRabbitTemplate;
    @Resource
    private RedisService redisService;
    @Resource
    private ReturnGoodsStaticsMapper returnGoodsStaticsMapper;

    /**
     * xinyichen的回调逻辑，处理短信发送结果
     */
    public void sendXinycMsgInfo(List<XinycSmsRequest> request) {
        guchuangRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_XYC, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        jumaiduoRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_XYC, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        yiquanRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_XYC, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        zxlpRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_XYC, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        ygyxRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_XYC, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        yxhdRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_XYC, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        jckjRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_XYC, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));

    }

    /**
     * 大汉的回调逻辑，处理短信发送结果
     */
    public void sendSantongSms(List<SanTongRequestVo> requestVos) {
        guchuangRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_SANTONG, JSONObject.toJSONString(requestVos, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        jumaiduoRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_SANTONG, JSONObject.toJSONString(requestVos, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        yiquanRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_SANTONG, JSONObject.toJSONString(requestVos, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        zxlpRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_SANTONG, JSONObject.toJSONString(requestVos, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        ygyxRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_SANTONG, JSONObject.toJSONString(requestVos, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        yxhdRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_SANTONG, JSONObject.toJSONString(requestVos, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        jckjRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_SANTONG, JSONObject.toJSONString(requestVos, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
    }

    /**
     * 梦网的回调逻辑，处理短信发送结果
     */
    public void sendMengWangSms(List<MengWangRequestVo> request) {
        guchuangRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_MENGWANG, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        jumaiduoRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_MENGWANG, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        yiquanRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_MENGWANG, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        zxlpRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_MENGWANG, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        ygyxRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_MENGWANG, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        yxhdRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_MENGWANG, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        jckjRabbitTemplate.convertAndSend(QueueConstants.MSG_CALLBACK_FOR_MENGWANG, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
    }

    /**
     * 发送短信
     */
    public void sendSms(SmsDto smsDto, String shopId) {
        if (shopId.equals("GUOB004") || shopId.equals("GUOB005") || shopId.equals("GUOB006")) {
            guchuangRabbitTemplate.convertAndSend(QueueConstants.SMS_QUEUE, smsDto);
        }
//        else if (shopId.equals("JUMD005") || shopId.equals("JUMD006")|| shopId.equals("JUMD007")) {
//            jumaiduoRabbitTemplate.convertAndSend(QueueConstants.SMS_QUEUE, smsDto);
//        } else if(shopId.equals("YQKJ006") || shopId.equals("YQKJ007") || shopId.equals("YQKJ008")) {
//            yiquanRabbitTemplate.convertAndSend(QueueConstants.SMS_QUEUE, smsDto);
//        } else if(shopId.equals("ZXLP005") || shopId.equals("ZXLP006") || shopId.equals("ZXLP007")) {
//            zxlpRabbitTemplate.convertAndSend(QueueConstants.SMS_QUEUE, smsDto);
//        } else if(shopId.equals("YGYX005") || shopId.equals("YGYX006") || shopId.equals("YGYX007") || shopId.equals("YGYX008")) {
//            ygyxRabbitTemplate.convertAndSend(QueueConstants.SMS_QUEUE, smsDto);
//        } else if(shopId.equals("YXHD005") || shopId.equals("YXHD006") || shopId.equals("YXHD007") || shopId.equals("YXHD008")) {
//            yxhdRabbitTemplate.convertAndSend(QueueConstants.SMS_QUEUE, smsDto);
//        } else if(shopId.equals("JCYX005") || shopId.equals("JCYX006") || shopId.equals("JCYX007") || shopId.equals("JCYX008")) {
//            jckjRabbitTemplate.convertAndSend(QueueConstants.SMS_QUEUE, smsDto);
//        }
    }

    /**
     * 统计单品售后统计
     */
    public Integer doProductRefundStatistics(ReturnOrderStatisticsRequest request) {
        long markId = System.currentTimeMillis();
        request.setMarkId(String.valueOf(markId));
        String key = RedisConstants.RETURN_GOOD_STATISTICS + markId;
        //设置MQ的数量,对应上面的MQ数量
        redisService.set(key,"7",24*3600L, TimeUnit.SECONDS);
        guchuangRabbitTemplate.convertAndSend(QueueConstants.RETURN_ORDER_STATISTICS_QUEUE, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        jumaiduoRabbitTemplate.convertAndSend(QueueConstants.RETURN_ORDER_STATISTICS_QUEUE, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        yiquanRabbitTemplate.convertAndSend(QueueConstants.RETURN_ORDER_STATISTICS_QUEUE, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        zxlpRabbitTemplate.convertAndSend(QueueConstants.RETURN_ORDER_STATISTICS_QUEUE, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        ygyxRabbitTemplate.convertAndSend(QueueConstants.RETURN_ORDER_STATISTICS_QUEUE, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        yxhdRabbitTemplate.convertAndSend(QueueConstants.RETURN_ORDER_STATISTICS_QUEUE, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
//        jckjRabbitTemplate.convertAndSend(QueueConstants.RETURN_ORDER_STATISTICS_QUEUE, JSONObject.toJSONString(request, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));

        ReturnGoodStatisticsSearch search = new ReturnGoodStatisticsSearch();
        search.setStartTime(request.getStartTime());
        search.setEndTime(request.getEndTime());
        search.setIsFinished(0);
        search.setMarkId(String.valueOf(markId));
        return returnGoodsStaticsMapper.insertSearch(search);
    }
}
